  function [facmed_mat,sample] =  out_timedraws(mat, sample, outpath , outname, addg, pvec ); 
% function [facmed_mat,cvec_temp] =  out_timedraws(mat, sample, outpath , outname, addg, pvec ); 
 
% mat          T x p x ndraws 
% sample       calendar vector ( T x 1) trimming inside the code
% outpath      Where to store it 
% Name         File
% pvec        vector of percentiles 
%             default is [0.1 0.32 0.68 0.9 ]

% 
% Alejandro Justiniano 10/24/04
% 3/08/2005  Input addg used to differentiate output name for graphs 
cucd = cd; 
if nargin < 6 
    pvec=[0.05 0.16  0.84 0.95]; 
end
if nargin < 5 
    flag_addg = 0 ; 
else 
    flag_addg = 1; 
    if isstr(addg) == 0 
        error('Addg must be a string'); 
    end 
end 

if nargin == 2 
    flag_save = 0 
else 
    if isstr( outname ) == 0 
        error('Name must be a string') 
    end 
    flag_save =1 
end 
sample=sample(:) ; 

pvec=pvec(:); 
nump=size( pvec , 1); 
if all( pvec > 1 ) == 1 
    pvec=pvec/100;
end
pvec_cell = num2cellaj( 100*pvec ); 

dim=size(mat); 
if dim(1)~= size( sample , 1); 
    error('Factor matrix and calendar matrix have different rows'); 
end 


fac_cell=emptycell(dim(1)+1, dim(2)*(nump+2)  + 1); 
fac_cell(2:end,1)=num2cellaj(sample); 
fac_cell(1,1) = {'Observations'}; 

numc=dim(2); 

top_cell=[]; 
cpf = nump+2; 
cpf_mat = 2:( size( fac_cell,2)  ); 
cpf_mat = reshape( cpf_mat , cpf , numc )'; 

med_mat = zeros( dim(1) , numc ); 


exind=round(dim(3)*pvec) ; 
hh=1; 
for hh=1:numc;
         
    tstr=['Ser',num2str(hh)];
    if flag_addg ==1
        tstr=[addg,' ',tstr]; 
    end 
    
    ii=1; 
    top_cell = cell( 1, nump ); 
    for ii=1:nump; 
        top_cell(ii)={[tstr,' ',num2str(pvec(ii)),'%']}; 
    end 
    pos_col = cpf_mat(hh,:); 
    
    top_cell=[{[tstr,' Mean'] , [tstr,' Med']} top_cell] ; 
    fac_cell( 1, pos_col  ) = top_cell ; 
    temp=squeeze( mat(:,hh,:) ); 
    %temp=reshape( temp,[dim(1) dim(3)*dim(4)] ); 
    temp=sort(temp,2); 
    fac_mean=squeeze( mean(temp,2) ) ;
    fac_med=squeeze( median(temp,2) ); 
    facmed_mat(:,hh) = fac_med; 
    
    fac_perc=temp( :, exind ); 
    fac_cell(2:end , pos_col(1) ) = num2cellaj( fac_mean); 
    fac_cell(2:end , pos_col(2) ) = num2cellaj( fac_med );
    fac_cell(2:end , pos_col(3:end) ) = num2cellaj( fac_perc );
    figure; 
    hold on; 
    plot( sample , fac_mean,'-b','LineWidth',1.5 ); 
    plot( sample , fac_med ,'--k','LineWidth',1.5 ); 
    plot( sample  , fac_perc(:,1), ':r','LineWidth',1); 
    plot( sample  , fac_perc(:,end), ':r','LineWidth',1);
    legend('mean','median',[pvec_cell{1},'%'],[pvec_cell{end},'%'] ); 
    title(tstr,'FontSize',10,'FontWeight','bold'); 
    hold off;
    if flag_save ==  1
        cd(outpath); 
        %save(['fac',tstr'],'fac_perc','fac_mean','pvec' ); 
        saveas(gcf,['g_',tstr],'fig'); 
        cd(cucd); 
    end 
    clear temp fac_mean fac_perc;         
end

if flag_save == 1
    cd(outpath)
    savecell(fac_cell,cucd,outpath,outname ); 
    disp('================='); 
    disp('Saved Cell'); 
end 

